<?php
declare (strict_types=1);

namespace app\index\controller;

use app\common\model\Assets;
use app\common\model\Bank;
use app\common\model\Relation;
use app\common\model\User as UserModel;
use app\common\model\UserNotify;
use app\common\tool\Result;
use think\exception\ValidateException;
use think\facade\View;
use think\Response;

class User extends Base
{
    public function index(): string
    {
        View::assign([
            'assets' => Assets::getFields(3),
            'order_1' => getOrderCount($this->user->id, 1),
            'order_2' => getOrderCount($this->user->id, 2),
            'order_3' => getOrderCount($this->user->id, 3),
            'notify_num' => UserNotify::where(['uid' => $this->user->id, 'status' => 0])->count()
        ]);

        return View::fetch();
    }

    public function setting(): string
    {
        return View::fetch();
    }

    public function info(): string
    {
        View::assign([
            'parent_code' => $this->user->parent_id ? UserModel::where(['id' => $this->user->parent_id])->value('code') : '',
        ]);

        return View::fetch();
    }

    public function edit(): string|Response
    {
        if ($this->request->isPost()) {
            $param = $this->request->post();

            return Result::fastReturn(UserModel::_edit($param, $this->user));
        }

        return View::fetch();
    }

    public function myTeam(): string|Response
    {
        if ($this->request->isAjax()) {
            $sort = [
                ['key' => 'is_active', 'value' => 'desc'],
                ['key' => 'active_time', 'value' => 'asc'],
                ['key' => 'id', 'value' => 'asc']
            ];

            return Result::fastReturn(UserModel::getPageList(['parent_id' => $this->user->id],
                'id,code,nickname,mobile,photo,my_total,team_total,level_id',
                $this->getPage(), $this->getLimit(), false, ['level'], [], $sort));
        }
        $team_count = Relation::where(['uid' => $this->user->id])->count();
        View::assign([
            'team_total' => $this->user->team_total,
            'team_count' => $team_count
        ]);

        return View::fetch();
    }

    public function methodList(): string
    {
        return View::fetch();
    }

    public function method($type): string
    {
        View::assign([
            'type' => $type,
            'title' => get_pay_way($type),
            'bank_list' => Bank::where(['status' => 1])->order('sort desc,id asc')->column('title', 'id')
        ]);

        return View::fetch();
    }

    public function methodSave($type): Response
    {
        try {
            $param = $this->request->post();
            $v = 'Method.' . $type;
            try {
                $this->validate($param, $v);
            } catch (ValidateException $e) {
                return Result::fail($e->getError());
            }
            switch ($type) {
                case "bank":
                case "usdt":
                    break;
                default:
                    $field_name = $type . '_name';
                    $field_image = $type . '_image';
                    $param[$field_name] = $param['name'];
                    $param[$field_image] = $param['image'];
                    unset($param['name']);
                    unset($param['image']);
            }
            UserModel::update($param, ['id' => $this->user->id]);

        } catch (\Exception $e) {

            return Result::fail($e->getMessage());
        }

        return Result::success('已保存');
    }

    public function contact(): string
    {
        return View::fetch();
    }

    public function share(): string
    {
        $url = request()->domain() . "/index/register/index.html?code=" . $this->user->code;
        View::assign('url', $url);

        return View::fetch();
    }

    public function logout(): Response
    {
        $token = md5(time() . $this->user['id'] . $this->request->ip());
        UserModel::update([
            'token' => $token
        ], ['id' => $this->user['id']]);
        cookie('token', null);

        return Result::fastReturn(['code' => 1, 'msg' => '已退出登录', 'url' => url('login/index')->build()]);
    }
}